package com.bobo.netty.server.websocketchat;

import com.bobo.netty.common.ConstantsRedis;
import com.bobo.netty.dao.chat.po.UserPO;
import com.bobo.netty.utils.RedisUtils;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class RedisProcess {
    @Resource
    private RedisUtils redisUtils;
    public String getUserName(Channel channel) {
        String channelId = channel.id().asLongText();
        //获取userID
        String userId = (String) redisUtils.getValueBykeys(ConstantsRedis.USER_CHANNEL_ID_PRE + channelId);
        return getUserName(userId);
    }
    public String getUserName(String userId) {
        UserPO userPO = (UserPO) redisUtils.getValueBykeys(ConstantsRedis.USER_ID_PRE + userId);
        if (userPO != null) {
            return userPO.getUserName();
        }
        return null;
    }

    public void removeChannelId(Channel channel) {
        String userId = (String) redisUtils.getValueBykeys(ConstantsRedis.USER_CHANNEL_ID_PRE + channel.id().asLongText());
        redisUtils.deleteValueBykeys(ConstantsRedis.USER_CHANNEL_ID_PRE + channel.id().asLongText());
        redisUtils.deleteValueBykeys(ConstantsRedis.USER_CHANNEL_ID_PRE + userId);
    }
}
